<?php

namespace app\common\model;

use think\Db;
use think\Model;


class SystemData extends Model
{

    // 表名
    protected $name = 'system_data';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = false;
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [

    ];


    /**
     * 变更平台积分
     * @param int    $money   余额
     * @param int    $user_id 会员ID
     * @param string $memo    备注
     */
    public static function points($type, $sn, $money, $memo, $admin_user_id = 0, $admin_id = 0)
    {
        Db::startTrans();
        try {
            $data = self::lock(true)->find(1);
            if ($data && $money != 0) {
                $before = $data->points;
                //$after = $user->money + $money;
                $after = function_exists('bcadd') ? bcadd($data->points, $money, 6) : $data->points + $money;
                //更新平台信息
                $data->save(['points' => $after]);
                //写入日志
                SystemPointsLog::create(['type' => $type, 'sn' => $sn, 'admin_id' => $admin_id, 'admin_user_id' => $admin_user_id, 'num' => $money, 'before' => $before, 'after' => $after, 'memo' => $memo]);
            }
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
        }
    }

    /**
     * 变更平台活动积分
     * @param int    $money   涉及余额
     * @param int    $num   手续费
     * @param int    $user_id 会员ID
     * @param string $memo    备注
     */
    public static function activity_points($type, $sn, $money, $num, $memo, $admin_user_id = 0, $admin_id = 0)
    {
        Db::startTrans();
        try {
            $data = self::lock(true)->find(1);
            if ($data && $num != 0) {
                $before = $data->activity_points;
                //$after = $user->money + $money;
                $after = function_exists('bcadd') ? bcadd($data->activity_points, $num, 6) : $data->activity_points + $num;
                //更新平台信息
                $data->save(['activity_points' => $after]);
                //写入日志
                SystemActivityPointsLog::create(['type' => $type, 'sn' => $sn, 'admin_id' => $admin_id, 'admin_user_id' => $admin_user_id, 'money' => $money, 'num' => $num, 'before' => $before, 'after' => $after, 'memo' => $memo]);
            }
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
        }
    }

    /**
     * 变更平台货值
     * @param int    $type    类型
     * @param int    $money   货值
     * @param int    $user_id 涉及会员ID
     * @param string $memo    备注
     */
    public static function cargo_value($type, $sn, $money, $memo, $user_id = 0, $admin_user_id = 0, $admin_id = 0)
    {
        Db::startTrans();
        try {
            $data = self::lock(true)->find(1);
            if ($data && $money != 0) {
                $before = $data->cargo_value;
                //$after = $user->money + $money;
                $after = function_exists('bcadd') ? bcadd($data->cargo_value, $money, 6) : $data->cargo_value + $money;
                //更新平台信息
                $data->save(['cargo_value' => $after]);
                //写入日志
                SystemCargoValueLog::create(['type' => $type, 'sn' => $sn, 'user_id' => $user_id,  'admin_id' => $admin_id, 'admin_user_id' => $admin_user_id, 'num' => $money, 'before' => $before, 'after' => $after, 'memo' => $memo]);
            }
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
        }
    }

    


    /**
     * 变更系统手续费
     * @param int    $money   手续费
     * @param int    $user_id 会员ID
     * @param string $memo    备注
     */
    public static function fee($money, $user_id, $memo)
    {
        Db::startTrans();
        try {
            $data = self::lock(true)->find(1);
            if ($data && $money != 0) {
                $user = User::get($user_id);
                $user_before = $user->activity_points;
                $user_after = function_exists('bcadd') ? bcadd($user->activity_points, $money, 6) : $user->activity_points + $money;
                $before = $data->fee;
                $after = function_exists('bcadd') ? bcadd($data->fee, $money, 6) : $data->fee + $money;
                //更新平台信息
                $data->save(['fee' => $after]);
                //写入日志
                SystemFeeLog::create(['user_id' => $user_id, 'user_before' => $user_before, 'user_after' => $user_after, 'num' => $money, 'before' => $before, 'after' => $after, 'memo' => $memo]);
            }
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
        }
    }


    public static function getVal($field = ''){
        $info = self::get(1);
        if($field && isset($info[$field])){
            return $info[$field];
        }else{
            return $info;
        }
    }




}
